<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
class  Cover
{
    public $return_data=['Code'=>1,'Msg'=>'','ErrorSql'=>'','Data'=>[]];
    // 分类
    public function category()
    {
           $p = Controller('Publics', 'logic');
           $res = $p->category();
           return json($res)->code(200)->header(['Content-Type' => 'application/json']);
    }
    // 上传图片-tmp
    public function uploadImg1(){
        $cover = isset($_FILES['cover']) ? $_FILES['cover'] : '';
        if(!$cover){
            $this->res_data['Msg']="上传错误1";
            return $this->print_result($this->res_data);
        }
        $pic_content = get_img_content($cover);
        $filename='goods_cover_'.uniqid()."_".mt_rand(10000,99999).'.png';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!file_put_contents($file, $pic_content)){
           $this->res_data['Msg']='服务器繁忙';
           return $this->res_data;
        }
        $c['cover'] = get_base_img($filepath);
        $c['path'] = $filepath;
        $this->res_data['Code']=0;
        $this->res_data['Msg']='成功';
        $this->res_data['Data']=$c;// 商品封面
        return $this->print_result($this->res_data);   
  
    }
    //上传 封面
    public function uploadImg(){
        $cover = isset($_FILES['cover']) ? $_FILES['cover'] : '';
        if(!$cover){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $maxSize = 10 * 1024 * 1024;
        if ($cover['size'] > $maxSize) {
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $filename='goods_cover_'.uniqid()."_".mt_rand(10000,99999).'.png';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!move_uploaded_file($cover['tmp_name'], $file)){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $c['cover'] = get_base_img($filepath);
        $c['path'] = $filepath;
        $this->res_data['Code']=0;
        $this->res_data['Msg']='成功';
        $this->res_data['Data']=$c;// 商品封面
        return $this->print_result($this->res_data);   
    }
    //上传视频
    public function uploadVideo(){
        $v = isset($_FILES['v']) ? $_FILES['v'] : '';
        if(!$v){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $maxSize = 10 * 1024 * 1024;
        if ($v['size'] > $maxSize) {
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $filename='goods_cover_'.uniqid()."_".mt_rand(10000,99999).'.mp4';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!move_uploaded_file($v['tmp_name'], $file)){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $c['video'] = get_base_img($filepath);
        $c['path'] = $filepath;
        $this->res_data['Code']=0;
        $this->res_data['Msg']='成功';
        $this->res_data['Data']=$c;// 商品封面
        return $this->print_result($this->res_data);   
    }
    public function print_result($return_data){
        if(!is_array($return_data)){
            $return_data['Code']=0;
            $return_data['Msg']='输出信息不符合规范';
        }
        return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
    }
}
